//
//  RingtonePlayer.m
//  
//
//  Created by zhenwei guan on 12-10-12.
//  Copyright (c) 2012. All rights reserved.
//

#import "RingtonePlayer.h"

@interface RingtonePlayer(Private) 

- (NSString *)getNameByIndex:(NSInteger)index;

@end

@implementation RingtonePlayer

@synthesize myPlayer;
@synthesize isLoop;
@synthesize loopMode;
@synthesize currentIndex;

#pragma mark init&dealloc
- (id)initWithIndex:(NSInteger)index andLoopMode:(BOOL)bIsLoop
{
    self = [super init];
    if (self)
    {
        NSString * ringtoneName = [self getNameByIndex:index];
        NSString * path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:ringtoneName];
        myPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:nil];
        if (bIsLoop) 
        {
            myPlayer.numberOfLoops = - 1;
        }
        else
        {
            myPlayer.numberOfLoops = 0;
        }
        self.isLoop = bIsLoop;
        myPlayer.delegate = self;
      
    }
    return self;
}

- (void)dealloc
{
    [myPlayer release];
    [super dealloc];
}

#pragma mark custom methods
- (void)startPlay
{
    
    if (myPlayer) 
    {
        if (self.isLoop) 
        {
            myPlayer.numberOfLoops = - 1;
        }
        else
        {
            myPlayer.numberOfLoops = 0;
        }

        [myPlayer play];  
    }
    else
    {
        NSLog(@"error,player nil in start play");
    }
}

- (void)stopPlay
{
    if (myPlayer) 
    {
        [myPlayer stop];  
    }
}

- (void)changeRingtoneToName:(NSString *)name
{
    if (myPlayer)
    {
        [myPlayer release];
        NSString * path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:name];
        myPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:nil];
        myPlayer.delegate = self;
        [self startPlay];
    }
    else
    {
        NSLog(@"error,player nil when changing");
    }
}

- (void)changeRingtoneToIndex:(NSInteger)index
{
    if (index > kRingtoneIndexNum-1) 
    {
        NSLog(@"error,index out of bound");
        return;
    }
    
    NSString * ringtoneName = [self getNameByIndex:index];
    [self changeRingtoneToName:ringtoneName];
    self.currentIndex = index;
}

- (NSString *)getNameByIndex:(NSInteger)index
{
    NSString * ringtoneName;
    switch (index)
    {
        case 0:
        {
            ringtoneName = @"beyond.mp3";
            break;
        }
        case 1:
        {
            ringtoneName = @"xintiao.mp3";
            break;
        }
        case 2:
        {
            ringtoneName = @"juebufangshou.mp3";
            break;
        }
     
        default:
        {
            ringtoneName = @"beyond.mp3";
            break;
        }
    }
    
    return ringtoneName;
}

#pragma mark global methods
+ (void)invokeSpeaker:(BOOL)open
{
	UInt32 route;
	UInt32 sessionCategory = kAudioSessionCategory_PlayAndRecord;
	AudioSessionSetProperty(kAudioSessionProperty_AudioCategory,
							sizeof(sessionCategory),
							& sessionCategory);
	route = open?kAudioSessionOverrideAudioRoute_Speaker:kAudioSessionOverrideAudioRoute_None;
	AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute,
							sizeof(route),
							&route);
}

#pragma mark avaudioPlayer delegate methods

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
    switch (loopMode)
    {
        case ShuffleMode:
            [self changeRingtoneToIndex:(rand()%kRingtoneIndexNum)];
            break;
        case RepeatMode:
            [self changeRingtoneToIndex:self.currentIndex];
            break;
        case OrderMode:
            [self changeRingtoneToIndex:(self.currentIndex+1)];
            break;
        default:
            [self changeRingtoneToIndex:(self.currentIndex+1)];
            break;
    }
    
}

@end
